<?php

// Acceso restringido
defined('JL') or die('Acceso restringido');

class LiensCSMEModel extends JLModelBase {

    /**
     * Funcion que retorna Lien
     * @Retorna array
     */
    public function getLiensById($id) {
        $_true = null;

        $sql = "SELECT 
              tbl_liens_csme.int_id_lien_csme,
              tbl_liens_csme.var_lien_title,
              tbl_liens_csme.var_lin_link,
              tbl_states.int_id_state
            FROM
              tbl_states
              INNER JOIN tbl_liens_csme ON (tbl_states.int_id_state = tbl_liens_csme.int_id_state) 
            WHERE tbl_liens_csme.int_id_lien_csme = " . $this->DB->Quote($id) . "";

        $this->DB->executeQuery($sql);

        if ($this->DB->getRowsAffected() > 0) {
            $_true = $this->DB->getDataArray();
            $_true = $_true[0];
        }

        return $_true;
    }

    /**
     * Actualiza Lien
     * @Retorna bool
     */
    public function updateLien($id, $var_lien_title, $var_lin_link, $int_id_sections_view, $int_id_state) {
        $_true = false;

        $sql = "UPDATE 
              `tbl_liens_csme`  
            SET
              `var_lien_title`  = " . $this->DB->Quote($var_lien_title) . ",
              `var_lin_link`    = " . $this->DB->Quote($var_lin_link) . ",
              `int_id_state`    = " . $this->DB->Quote($int_id_state) . "
            WHERE 
              `int_id_lien_csme` = " . $this->DB->Quote($id) . "";

        $this->DB->executeQuery($sql);

        if ($this->DB->getRowsAffected() > 0) {
            $_true = true;
        }

        return $_true;
    }

    /**
     * Inserta Lien
     * @Retorna bool
     */
    public function insertLien($var_lien_title, $var_lin_link, $int_id_sections_view, $int_id_state) {
        $_true = false;

        $sql = "INSERT INTO 
              `tbl_liens_csme`
            (
              `var_lien_title`,
              `var_lin_link`,
              `int_id_state`
            ) 
            VALUE(
              " . $this->DB->Quote($var_lien_title) . ",
              " . $this->DB->Quote($var_lin_link) . ",
              " . $this->DB->Quote($int_id_state) . "
            )";

        $this->DB->executeQuery($sql);

        if ($this->DB->getRowsAffected() > 0) {
            $_true = true;
        }

        return $_true;
    }

    /**
     * Elimina Lien
     * @Retorna bool
     */
    public function deleteLien($id) {
        $_true = false;

        $sql = "DELETE FROM 
              `tbl_liens_csme` 
            WHERE 
              `int_id_lien_csme` = " . $this->DB->Quote($id) . " AND `int_id_state` <> 1";

        $this->DB->executeQuery($sql);

        if ($this->DB->getRowsAffected() > 0) {
            $_true = true;
        }

        return $_true;
    }

}